﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class HangarView : MonoBehaviour {

	public GameObject hangar;
	public PlayerStorage main;
	public GameObject MainCamera;
	
	public Text VehicleTitle;
	public Text VehiclePrice;
	public Text VehicleDescription;
	
	public GameObject ActiveVehicle;
	public int vehicle = 1;
	float vehicleCost;
	public Button BuyButton;
	
	public List<GameObject> Vehicles;
// swap out activevehicle instead of moving camera. 
// set and names based on vehicle prefab
// build list of purchaseable vehicles.
	
	
	void SwitchVehicle(int x)
	{
		
		if (vehicle == x)
		{
			for (int a =0; a < Vehicles.Count; a++)
			{
				Vehicles[a].SetActive(false);
				
			}
			Vehicles[x].SetActive(true); 
			ActiveVehicle = Vehicles[x];
			VehicleTitle.text = Vehicles[x].GetComponent<HangarVehicle>().Name;
			VehiclePrice.text = "$" + Vehicles[x].GetComponent<HangarVehicle>().Price.ToString();
			VehicleDescription.text = Vehicles[x].GetComponent<HangarVehicle>().Description;
			vehicleCost = ActiveVehicle.GetComponent<HangarVehicle>().Price;
			checkOwnership();
			
			
		}
	
	}
	
	
	public void BuyVehicle()
	{
		
		if (main.cash > vehicleCost)
		{
			main.cash += -vehicleCost;
			if (vehicle == 0)
			{
				main.trukkOwned = 1;
			}
			if (vehicle == 1)
			{
				main.pickupOwned = 1;
			}
			if (vehicle == 2)
			{
				main.buggyOwned = 1;
			}
			
		}
	
	}
	
	public void PurchaseVehicle()
	{
		if (vehicle == 0)
		{
			if (main.trukkOwned == 0)
			{
				BuyVehicle();
			}
		}
		if (vehicle == 1)
		{
			if (main.pickupOwned == 0)
			{
				BuyVehicle();
			}
		}
		if (vehicle == 2)
		{
			if (main.buggyOwned == 0)
			{
				BuyVehicle();
			}
		}
		
	}
	
	void checkOwnership()
	{
		if (vehicle == 0)
		{
			if (main.trukkOwned == 1)
			{
				main.activeVehicle = 0;
			}
		}
		if (vehicle == 1)
		{
			if (main.pickupOwned == 1)
			{
				main.activeVehicle = 1;
			}
		}
		if (vehicle == 2)
		{
			if (main.buggyOwned == 1)
			{
				main.activeVehicle = 2;
			}
		}
	}
	
	
	public void SwitchPaint()
	{
		
		main.vehiclePaint ++;
		if (main.vehiclePaint > ActiveVehicle.GetComponent<HangarVehicle>().PaintJobs.Count-1)
		{
			main.vehiclePaint = 0;
		}

		foreach (Transform child in ActiveVehicle.transform)
		{
			if (child.gameObject.renderer != null)
			{
				if (child.gameObject.renderer.material != null)
				{
					child.gameObject.renderer.material = ActiveVehicle.GetComponent<HangarVehicle>().PaintJobs[main.vehiclePaint];;
				}
			}
		}
	}
	
	
	
//	void SwitchPositions()
//	{
//		switch (cameraPosition)
//		{
//			case 0:
//			{
//				MainCamera.transform.position = (Pos_01.position);
//				break;
//			}
//			case 1:
//			{
//				MainCamera.transform.position = (Pos_02.position);
//				break;
//			}
//			case 2:
//			{
//				MainCamera.transform.position =  (Pos_03.position);
//				break;
//			}
//		}
//
//	}
	
	public void MoveCameraUp()
	{
		if (vehicle < 2)
		{
			vehicle ++;
			SwitchVehicle(vehicle);
		}
		else 
		{
			vehicle = 0;
			SwitchVehicle(vehicle);
		}
	}
	
	public void MoveCameraDown()
	{
		if( vehicle > 0)
		{
			vehicle --;
			SwitchVehicle(vehicle);
		}
		else 
		{
			vehicle = 2;
			SwitchVehicle(vehicle);
		}
	}
	


	void OnEnable()
	{
	
		
		SwitchVehicle(vehicle);
		
		hangar.SetActive(true);
	}
	
	void OnDisable()
	{
		hangar.SetActive(false);
	}
	
	void Update()
	{
		
		if (vehicle == 0 && main.trukkOwned == 1)
		{
			VehiclePrice.text = "Owned";
			BuyButton.interactable = false;
		}
		if (vehicle == 0 && main.trukkOwned == 0)
		{
			BuyButton.interactable = true;
		}
		if (vehicle == 1 && main.pickupOwned == 1)
		{
			VehiclePrice.text = "Owned";
			BuyButton.interactable = false;
		}
		if (vehicle == 1 && main.pickupOwned == 0)
		{
			BuyButton.interactable = true;
		}
		if (vehicle == 2 && main.buggyOwned == 1)
		{
			VehiclePrice.text = "Owned";
			BuyButton.interactable = false;
		}
		if (vehicle == 2 && main.buggyOwned == 0)
		{
			BuyButton.interactable = true;
		}
		if (main.cash > vehicleCost)
		{
			BuyButton.interactable = true;
		}
		if (main.cash < vehicleCost)
		{
			BuyButton.interactable = false;
		}
	}
	
}
